<template>
  <div id="login" style="background-image: url('./src/assets/login.jpg')">
    <div class="me-login-box me-login-box-radius">
      <h1>后台管理系统</h1>
      <el-form ref="userForm" :model="userForm" :rules="rules">
        <el-form-item prop="username">
          <el-input placeholder="用户名" v-model="userForm.username"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input placeholder="密码" type="password" v-model="userForm.password"></el-input>
        </el-form-item>

        <el-form-item size="small" class="me-login-button">
          <el-button type="primary" @click.native.prevent="login('userForm')">登录</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        userForm: {
          username: '',
          password: ''
        },
        rules: {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'},
            {max: 10, message: '不能大于10个字符', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {max: 10, message: '不能大于10个字符', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      login(formName) {

        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = 'http://localhost:9081/admins/login';
            console.log('url = ' + url);

            let formData = this.qs.stringify(this. userForm);
            console.log('formData = ' + formData);

            this.axios.post(url, formData).then((response) => {
              let responseBody = response.data;
              console.log(responseBody);
              if (responseBody.state == 20000) {
                this.$message({
                  showClose: true,
                  message: '登录成功！',
                  type: 'success'
                });
                let jwt = responseBody.data;
                console.log('jwt = ' + jwt);
                localStorage.setItem('jwt', jwt);
                console.log('已经将JWT存入到localStorage中，即将跳转页面……');
                this.$router.push('/admin');
              } else {
                this.$message({
                  showClose: true,
                  message: responseBody.message,
                  type: 'error'
                });
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style scoped>
  #login {
    min-width: 100%;
    min-height: 100%;
  }

  .me-login-box {
    position: absolute;
    width: 300px;
    height: 260px;
    background-color: white;
    margin-top: 150px;
    margin-left: -180px;
    left: 50%;
    padding: 30px;
  }

  .me-login-box-radius {
    border-radius: 10px;
    box-shadow: 0px 0px 1px 1px rgba(161, 159, 159, 0.1);
  }

  .me-login-box h1 {
    text-align: center;
    font-size: 24px;
    margin-bottom: 20px;
    vertical-align: middle;
  }


  .me-login-button {
    text-align: center;
  }

  .me-login-button button {
    width: 100%;
  }

</style>
